﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYElectricSetting;

namespace HYRElectric.WirePlace
{
	public partial class NewWireParamForm : System.Windows.Forms.Form
	{
		public NewWireParamForm(Document m_doc)
		{
			this.InitializeComponent();
			this.nudWireNum.TextChanged += this.nudWireNum_TextChanged;
			this.doc = m_doc;
		}

		public void InitializeDefaultParameters()
		{
			this.xmlHelper.LoadXml(this.path, "Root");
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeWireConduitInfo();
			this.FillFormData();
			this.LoadDlgData();
		}

		private void WireParamForm_Load(object sender, EventArgs e)
		{
			this.xmlHelper.LoadXml(this.path, "Root");
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeWireConduitInfo();
			this.FillFormData();
			this.LoadDlgData();
		}

		private void InitializeWireConduitInfo()
		{
			this.revitWireTypes = ElectricSettingDataAssist.GetRevitWireTypes(this.doc);
			this.revitConduitTypes = ElectricSettingDataAssist.GetRevitConduitTypes(this.doc);
			if (this.revitWireTypes.Count == 0)
			{
				throw new Exception("请先进行导线设置。");
			}
			if (this.revitConduitTypes.Count == 0)
			{
				throw new Exception("请先进行线管设置。");
			}
		}

		private void WireParamForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (this.isSave)
			{
				this.SaveDlgData();
			}
		}

		private void FillFormData()
		{
			try
			{
				foreach (RevitWireType item in this.revitWireTypes)
				{
					this.cmbWireType.Items.Add(item);
				}
				this.cmbWireType.SelectedIndex = 0;
				this.nudWireNum.Value = 1m;
				WireConnectForm.wireParams.WireNum = 1;
				this.cmbLayMode.Items.Add("AB");
				this.cmbLayMode.Items.Add("BC");
				this.cmbLayMode.Items.Add("AC");
				this.cmbLayMode.Items.Add("CLC");
				this.cmbLayMode.Items.Add("WS");
				this.cmbLayMode.Items.Add("WC");
				this.cmbLayMode.Items.Add("CE");
				this.cmbLayMode.Items.Add("CC");
				this.cmbLayMode.Items.Add("SCE");
				this.cmbLayMode.Items.Add("FC");
				this.cmbLayMode.SelectedIndex = 0;
				this.txtLevel.Text = "3";
			}
			catch (Exception)
			{
				TaskDialog.Show("盈建科提示", "读取数据出错。");
				throw;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.isSave = false;
			base.Close();
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "Params"));
			string attribute = this.xmlHelper.GetAttribute("WireType");
			if (!string.IsNullOrEmpty(attribute))
			{
				foreach (RevitWireType revitWireType in this.revitWireTypes)
				{
					if (revitWireType.Name == attribute)
					{
						this.cmbWireType.SelectedItem = revitWireType;
						break;
					}
				}
			}
			if (!string.IsNullOrEmpty(this.xmlHelper.GetAttribute("WireNum")))
			{
				this.nudWireNum.Value = Convert.ToInt32(this.xmlHelper.GetAttribute("WireNum"));
			}
			if (this.cmbWireType.Items.Count > 0)
			{
				string attribute2 = this.xmlHelper.GetAttribute("CrossSection");
				if (!string.IsNullOrEmpty(attribute2))
				{
					this.cmbCrossSection.Text = attribute2;
				}
			}
			string attribute3 = this.xmlHelper.GetAttribute("LayMode");
			if (!string.IsNullOrEmpty(attribute3))
			{
				this.cmbLayMode.SelectedItem = attribute3;
			}
			string attribute4 = this.xmlHelper.GetAttribute("Level");
			if (!string.IsNullOrEmpty(attribute4))
			{
				this.txtLevel.Text = attribute4;
			}
			string attribute5 = this.xmlHelper.GetAttribute("ConduitTypeID");
			if (!string.IsNullOrEmpty(attribute5))
			{
				foreach (object obj in this.cmbConduitType.Items)
				{
					if ((obj as RevitConduitType).ConduitTypeId.ToString() == attribute5)
					{
						this.cmbConduitType.SelectedItem = obj;
						break;
					}
				}
			}
			if (this.cmbConduitType.Items.Count > 0)
			{
				string attribute6 = this.xmlHelper.GetAttribute("ConduitDiameter");
				if (!string.IsNullOrEmpty(attribute6))
				{
					this.cmbConduitDiameter.Text = attribute6;
				}
			}
		}

		public void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "Params"));
			this.xmlHelper.SetAttribute("WireType", this.cmbWireType.SelectedItem.ToString());
			this.xmlHelper.SetAttribute("WireNum", this.nudWireNum.Value.ToString());
			this.xmlHelper.SetAttribute("CrossSection", this.cmbCrossSection.Text);
			this.xmlHelper.SetAttribute("LayMode", this.cmbLayMode.SelectedItem.ToString());
			this.xmlHelper.SetAttribute("Level", this.txtLevel.Text.Trim());
			this.xmlHelper.SetAttribute("ConduitTypeID", ((RevitConduitType)this.cmbConduitType.SelectedItem).ConduitTypeId.ToString());
			this.xmlHelper.SetAttribute("ConduitDiameter", this.cmbConduitDiameter.Text);
			this.xmlHelper.SaveXml("");
		}

		private void UpdateConduitDiameter()
		{
			if (this.cmbWireType.Items.Count > 0 && this.cmbConduitType.Items.Count > 0 && !string.IsNullOrEmpty(this.cmbCrossSection.Text) && this.cmbConduitDiameter.Items.Count > 0)
			{
				int conduitDiameter = ElectricSettingDataAssist.GetConduitDiameter(this.doc, this.cmbWireType.SelectedItem.ToString(), Convert.ToDouble(this.cmbCrossSection.Text), this.cmbConduitType.SelectedItem.ToString());
				double num = (double)this.FindProximal(this.cmbConduitType.SelectedItem.ToString(), conduitDiameter);
				this.cmbConduitDiameter.Text = num.ToString();
			}
		}

		private int FindProximal(string conduitType, int target)
		{
			List<int> list = new List<int>();
			foreach (object obj in this.cmbConduitType.Items)
			{
				RevitConduitType revitConduitType = obj as RevitConduitType;
				if (revitConduitType.Name == conduitType)
				{
					list = revitConduitType.ConduitSizeSet;
					break;
				}
			}
			List<int> list2 = new List<int>();
			if (list.Count > 0)
			{
				int num = Math.Abs(target - list[0]);
				foreach (int num2 in list)
				{
					int num3 = Math.Abs(target - num2);
					if (num3 < num)
					{
						num = num3;
						list2.Clear();
						list2.Add(num2);
					}
					else if (num3 == num)
					{
						list2.Add(num2);
					}
				}
			}
			if (list2.Count > 0)
			{
				return list2[0];
			}
			if (list.Count <= 0)
			{
				return 0;
			}
			return list[0];
		}

		private void cmbWireType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cmbCrossSection.Items.Clear();
			WireConnectForm.wireParams.WireType = this.cmbWireType.SelectedItem.ToString();
			RevitWireType revitWireType = this.cmbWireType.SelectedItem as RevitWireType;
			foreach (double num in revitWireType.WireSizeSet.Keys)
			{
				this.cmbCrossSection.Items.Add(num);
			}
			if (revitWireType.WireSizeSet.Count > 0)
			{
				this.cmbCrossSection.SelectedIndex = 0;
			}
			else
			{
				this.cmbCrossSection.Text = "4";
			}
			List<RevitConduitType> conduitTypesForWire = ElectricSettingDataAssist.GetConduitTypesForWire(this.doc, WireConnectForm.wireParams.WireType);
			if (conduitTypesForWire.Count > 0)
			{
				this.cmbConduitType.Items.Clear();
				foreach (RevitConduitType item in conduitTypesForWire)
				{
					this.cmbConduitType.Items.Add(item);
				}
				this.cmbConduitType.SelectedIndex = 0;
			}
			else
			{
				this.cmbConduitType.Items.Clear();
				foreach (RevitConduitType item2 in this.revitConduitTypes)
				{
					this.cmbConduitType.Items.Add(item2);
				}
				this.cmbConduitType.SelectedIndex = 0;
			}
			this.UpdateConduitDiameter();
		}

		private void nudWireNum_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.Control control = sender as System.Windows.Forms.Control;
			if (string.IsNullOrEmpty(control.Text))
			{
				TaskDialog.Show("盈建科提示", "导线根数参数不能为空。");
				control.Text = "1";
			}
			int wireNum;
			if (int.TryParse(this.nudWireNum.Value.ToString().Trim(), out wireNum) && this.nudWireNum.Value >= 1m)
			{
				WireConnectForm.wireParams.WireNum = wireNum;
				this.UpdateConduitDiameter();
			}
		}

		private void cmbCrossSection_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.CrossSection = Convert.ToDouble(this.cmbCrossSection.SelectedItem.ToString());
			this.UpdateConduitDiameter();
		}

		private void cmbCrossSection_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cmbCrossSection.Text))
			{
				TaskDialog.Show("盈建科提示", "截面不能为空。");
				this.cmbCrossSection.Text = "4";
				this.cmbCrossSection.Focus();
				return;
			}
			WireConnectForm.wireParams.CrossSection = Convert.ToDouble(this.cmbCrossSection.Text);
			this.UpdateConduitDiameter();
		}

		private void cmbLayMode_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.LayMode = this.cmbLayMode.SelectedItem.ToString();
		}

		private void txtLevel_TextChanged(object sender, EventArgs e)
		{
			if (this.txtLevel.Text.Length > 0)
			{
				double level;
				if (this.txtLevel.Text == "-")
				{
					level = 0.0;
				}
				else
				{
					level = Convert.ToDouble(this.txtLevel.Text.Trim());
				}
				WireConnectForm.wireParams.Level = level;
				return;
			}
			TaskDialog.Show("盈建科提示", "相对标高参数不能为空。");
			this.txtLevel.Text = "3.5";
			this.txtLevel.SelectAll();
		}

		private void txtLevel_KeyPress(object sender, KeyPressEventArgs e)
		{
			Common.IsDouble(e, this.txtLevel);
		}

		private void cmbConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.ConduitType = this.cmbConduitType.SelectedItem.ToString();
			RevitConduitType revitConduitType = this.cmbConduitType.SelectedItem as RevitConduitType;
			WireConnectForm.wireParams.ConduitTypeID = revitConduitType.ConduitTypeId;
			List<int> conduitSizeSet = revitConduitType.ConduitSizeSet;
			this.cmbConduitDiameter.Items.Clear();
			if (conduitSizeSet.Count > 0)
			{
				foreach (int num in conduitSizeSet)
				{
					this.cmbConduitDiameter.Items.Add(num);
				}
				this.cmbConduitDiameter.SelectedIndex = 0;
			}
			else
			{
				this.cmbConduitDiameter.Text = "16";
			}
			this.UpdateConduitDiameter();
		}

		private void cmbConduitDiameter_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cmbConduitDiameter.Text))
			{
				TaskDialog.Show("盈建科提示", "管径不能为空。");
				this.cmbConduitDiameter.Text = "16";
				this.cmbConduitDiameter.Focus();
				return;
			}
			WireConnectForm.wireParams.ConduitDiameter = Convert.ToDouble(this.cmbConduitDiameter.Text);
		}

		private void cmbConduitDiameter_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.ConduitDiameter = Convert.ToDouble(this.cmbConduitDiameter.SelectedItem.ToString());
		}

		private void cmbCrossSection_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.cmbCrossSection.Text);
		}

		private void cmbConduitDiameter_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.cmbConduitDiameter.Text);
		}

		public void IsPlusDouble(KeyPressEventArgs e, string txt)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "ConnectWire.xml");

		private const string Root = "Root";

		private const string Node = "HYConnectWire";

		private const string ChildNode = "Params";

		private const string WireType = "WireType";

		private const string WireNum = "WireNum";

		private const string CrossSection = "CrossSection";

		private const string LayMode = "LayMode";

		private const string Level = "Level";

		private const string ConduitDiameter = "ConduitDiameter";

		private const string ConduitTypeID = "ConduitTypeID";

		private Document doc;

		private bool isSave = true;

		private List<RevitWireType> revitWireTypes = new List<RevitWireType>();

		private List<RevitConduitType> revitConduitTypes = new List<RevitConduitType>();
	}
}
